package lk.icta.common.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: it-front
 * Date: 6/23/14
 * Time: 1:47 PM
 * To change this template use File | Settings | File Templates.
 */
public class MigrationTool {

    public static int getDistrictId(String dis){
        String d = dis.trim().toUpperCase();
        int id = 0;
        if(d.equals("NULL")){
            id = 23;
        }else if(d.equals("COL")){ // Colombo
            id = 1;
        }else if(d.equals("ANP")){ // Anuradhapura
            id = 20;
        }else if(d.equals("BAD")){ // Badulla
            id = 22;
        }else if(d.equals("BCO")){ // Batticaloa
            id = 15;
        }else if(d.equals("GPH")){ // Gampaha
            id = 2;
        }else if(d.equals("JFN")){ // Jaffna
            id = 10;
        }else if(d.equals("KDT")){ // Kandy
            id = 4;
        }else if(d.equals("KGE")){ // Kegalle
            id = 25;
        }else if(d.equals("KRN")){ // Kurunegala
            id = 18;
        }else if(d.equals("KTR")){ // Kaluthara
            id = 3;
        }else if(d.equals("MAN")){ // Mannar
            id = 11;
        }else if(d.equals("MTR")){ // Matara
            id = 8;
        }else if(d.equals("NYA")){ // Nuwaraeliya
            id = 6;
        }else if(d.equals("PLN")){ // Polonnaruwa
            id = 21;
        }else if(d.equals("RPR")){ // Rathnapura
            id = 24;
        }else if(d.equals("TCO")){ // Trincomalee
            id = 17;
        }else if(d.equals("VNA")){ // Vauniya
            id = 12;
        }else{
            id = 23;
        }
        return id;
    }

    public static int getUsageId(String code){
        code = code.toUpperCase();
        int id = 0;

        if(code.equals("AGRI")){
            id = 1;
        }else if(code.equals("BUSS")){
            id = 2;
        }else if(code.equals("RELG")){
            id = 3;
        }else if(code.equals("SPORT")){
            id = 4;
        }else if(code.equals("SHOP")){
            id = 5;
        }else if(code.equals("IND")){
            id = 6;
        }else if(code.equals("LND")){
            id = 7;
        }else if(code.equals("OFIC")){
            id = 8;
        }else if(code.equals("WAT")){
            id = 9;
        }else if(code.equals("ELE")){
            id = 10;
        }else if(code.equals("TLE")){
            id = 11;
        }else if(code.equals("LVC")){
            id = 12;
        }else if(code.equals("RD")){
            id = 13;
        }else if(code.equals("BLD")){
            id = 14;
        }else if(code.equals("ENC")){
            id = 15;
        }else if(code.equals("RES")){
            id = 16;
        }else if(code.equals("OTR")){
            id = 17;
        }else{
            id = 2; // Default if unidentified
        }

        return id;
    }

    public static int getRailwayTrackId(String code){
        code = code.toUpperCase().trim();
        int id=0;
        if(code.equals("CL")){
            id = 1;
        }else if(code.equals("ML")){
            id = 2;
        }else if(code.equals("PL")){
            id = 3;
        }else if(code.equals("NL")){
            id = 4;
        }else if(code.equals("BTL")){
            id = 5;
        }else if(code.equals("TCL")){
            id = 6;
        }else if(code.equals("HL")){
            id = 7;
        }else if(code.equals("KVL")){
            id = 8;
        }else if(code.equals("MTL")){
            id = 9;
        }
        return id;
    }

    public static int getDeZoneId(String code){
        code = code.toUpperCase().trim();
        int id = 1;                   // Default is 1
        if(code.equals("DEC")){
            id = 1;
        }else if(code.equals("DEL")){
            id = 2;
        }else if(code.equals("DEU")){
            id = 3;
        }else if(code.equals("DEN")){
            id = 4;
        }else if(code.equals("DEE")){
            id = 5;
        }
        return id;
    }

    /**
     * Resolves and extracts mileage locaction data of the land
     * @param loc
     * @return
     */
    public static List<String> resolveLocationMiles(String loc){
        List<String> locationMile = new ArrayList<String>();
        if(loc.matches(".*\\d.*")){ // if location miles contains digits

            if(loc.contains("&")){
                String[] locMilesexcel = loc.split("&");
                if(locMilesexcel.length==2){
                    locationMile.add(locMilesexcel[0].trim());
                    locationMile.add(locMilesexcel[1].trim());
                }else{
                    locationMile.add(loc.trim());
                }
            }else{
                String[] locMilesdb= loc.split(" ");
                if(locMilesdb.length==4){
                    locationMile.add(locMilesdb[0]+" "+locMilesdb[1]);
                    locationMile.add(locMilesdb[2]+" "+locMilesdb[3]);
                }else{
                    locationMile.add(loc.trim());
                }
            }
        }else {
            locationMile.add(loc.trim());
        }
        return locationMile;
    }

    /**
     *
     * @param fNo
     * @return
     */
    public static String getFileNo(String fNo){
        return fNo.trim().toUpperCase();
    }

    public static double[] getMilesAndChain(String loc) throws Exception{
        try{
            String[] l = loc.split(" ");
            String mile = l[0].trim();
            String chain = l[1].trim();

            mile = mile.substring(0, mile.length() - 1);
            chain = chain.substring(0, chain.length() - 1);

            double m = Double.parseDouble(mile);
            double c = Double.parseDouble(chain);
            double[] lo ={m,c};
            return lo;
        }catch(Exception e){
            throw new Exception();
        }
    }

    public static void printRow(String[] land) {
        System.out.print("[");
        for(String s : land){
            System.out.print(", "+s);
        }
        System.out.print("]\n\n");
    }

    public static String getPlanNo(String pNo){
        String p = pNo.trim().toUpperCase();
        if(p.equals("NULL")){
            p = "";
        }
        return p;
    }

    public static String getServeyorName(String s){
        String svn = s.trim();
        if(svn.equals("Null")){
            svn = "";
        }
        return svn;
    }

    public static String getVillage(String vil){
        String v = vil.trim();
        if(v.equals("Null")){
            v = "";
        }
        return v;
    }

    public static String getPlanDate(String dt){
        String d = dt.trim().replace('/', '-');
        if(d.equals("Null")){
            d = "";
        }
        return d;
    }

    public static Date getDate(String dt){
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy.MM.dd", Locale.ENGLISH).parse(dt);
        } catch (ParseException e) {
            date = new Date();
        }
        return date;
    }

    public static double getMilegeValue(String val){
        double d = 0;
        try{
            val  = val.trim().replaceAll("\\D+","");
            d = Double.parseDouble(val);
        }catch (Exception e){

        }
        return d;
    }

    public static String getCleanStringUpperCase(String code){
       return code.trim().toUpperCase();
    }

    public static int getValidityPeriod(String s) {
           s=s.trim().replaceAll("\\D+","");
           int x = 0;
            try{
                x = Integer.parseInt(s);
            }catch (Exception e){
                x = 0;
            }
        return x;
    }

    public static double getAreaInPerchs(String acr, String rd, String per) {
        double a = (acr.equals("")) ? 0d : Double.parseDouble(acr.replaceAll("\\D+",""));
        double r = (rd.equals("")) ? 0d : Double.parseDouble(rd.replaceAll("\\D+",""));
        double p = (per.equals("")) ? 0d :Double.parseDouble(per.replaceAll("\\D+",""));
        return (a*160 + r*40 + p);
    }

    public static double getAddedPercentage(String s) {
        s = s.trim().toUpperCase();
        return s.equals("YES") ? 50 : 0 ;
    }

    public static Double getValuationAmount(String s) {
        s = s.trim().replaceAll("\\D+", "");
        return s.equals("") ? 0 : Double.parseDouble(s);
    }

    public static int getAddedPercentageState(String s) {
        s = s.trim().toUpperCase();
        return s.equals("YES") ? 1 : 0 ;
    }
}
